<template>
<transition name="dialog-fade">
  <div class="l-dialog_wrapper" v-show="visible" @click.self="handleClose">
    <div class="l-dialog" :style="{width:width, marginTop:top}">
      <div class="l-dialog_header">
        <slot name="title">
          <span class="l-dialog_title">{{ title }}</span>
        </slot>
        <button class="l-dialog_headerbtn" @click="handleClose">
          <i class="one-icon-close"></i>
        </button>
      </div>
      <div class="l-dialog_body">
        <slot></slot>
      </div>
      <div class="l-dialog_footer" v-if="$slots.footer">
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
  </transition>
</template>

<script>
export default {
  name: 'LDialog',
  props: {
    title: {
      type: String,
      default: '标题'
    },
    width: {
      type: String,
      default: '50%'
    },
    top: {
      type: String,
      default: '15vh'
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  created () {
    console.log(this.width, this.top)
  },
  methods: {
    handleClose () {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.l-dialog_wrapper {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
  margin: 0;
  z-index: 2001;
  background-color: rgba(0, 0, 0, 0.5);
  .l-dialog {
    position: relative;
    margin: 15vh auto 50px;
    background: #fff;
    border-radius: 2px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    box-sizing: border-box;
    width: 30%;
    &_header {
      padding: 20px 20px 10px;
      .l-dialog_title {
        line-height: 24px;
        font-size: 18px;
        color: #303133;
      }
      .l-dialog_headerbtn {
        position: absolute;
        top: 20px;
        right: 20px;
        padding: 0;
        background: transparent;
        border: none;
        outline: none;
        cursor: pointer;
        font-size: 16px;
        .l-icon-close {
          color: 909399;
        }
      }
    }
    &_body {
      padding: 30px 20px;
      color: #606266;
      font-size: 14px;
      word-break: break-all;
    }
    &_footer {
      padding: 10px 20px 20px;
      text-align: right;
      box-sizing: border-box;
      ::v-deep .l-button:first-child {
        margin-right: 20px;
      }
    }
  }
}
// dialog的动画
.dialog-fade-enter-active{
  animation: run .5s;
}
.dialog-fade-leave-active{
  animation: run .3s reverse;
}
@keyframes run {
  0% {
      opacity: 0;
      transform: translateY(-30px);
  }
  100% {
      opacity: 1;
      transform: translateY(0px);
  }
}
</style>
